package com.dongdongshop.controller;


import com.dk.data.R;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//Es搜索模糊查询高亮显示

@Controller
@RequestMapping("Es")
public class EsController {


    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @RequestMapping("tiao")
    public String tiao(String name){
        stringRedisTemplate.boundValueOps("names").set(name);
        return "index-page";
    }





    @RequestMapping("getall")
    @ResponseBody
    public R getall(Integer page) throws IOException {
        String name = stringRedisTemplate.boundValueOps("names").get();

        // 搜索请求对象
        SearchRequest searchRequest = new SearchRequest("dongdongshop");
        // 搜索源构建对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //int page = 1; // 页码
        Integer size = 10; // 每页显示的条数
        Integer index = (page - 1) * size;
        searchSourceBuilder.from(index);


        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", name);//搜索条件
        // 模糊查询
        // 全部查询
        searchSourceBuilder.query(matchQueryBuilder);
        // 设置源字段过虑,第一个参数结果集包括哪些字段，第二个参数表示结果集不包括哪些字段
        searchSourceBuilder.fetchSource(new String[]{"id", "title", "image","price"}, new String[]{});
        // 向搜索请求对象中设置搜索源
        searchRequest.source(searchSourceBuilder);

        //高亮显示
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //设置高亮前缀
        highlightBuilder.preTags("<span style='color:red' >");//修饰为红色
        //设置高亮字段
        highlightBuilder.field("title");
        //设置高亮后缀
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);

        String queryJson = searchSourceBuilder.toString();
        System.out.println(queryJson);

        // 执行搜索,向ES发起http请求
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 搜索结果
        SearchHits hits = searchResponse.getHits();
        List ls=new ArrayList();
        for (SearchHit hit : hits.getHits()) {
            // 源文档内容
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //高粱字段
            Map<String, HighlightField> highlightFields=hit.getHighlightFields();
            HighlightField title=highlightFields.get("title");
            //替换
            if(title !=null){
                //获取当前高亮字段
                Text[] fragments = title.getFragments();
                StringBuilder stringBuilder = new StringBuilder();
                for (Text text : fragments) {
                    stringBuilder.append(text);
                }
                sourceAsMap.put("title",stringBuilder.toString());
            }
            System.out.println(sourceAsMap);
            ls.add(sourceAsMap);
        }
        return R.ok().setData(ls);
    }














}
